﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.Utility;
using Aliyeye.Car.Net.UI.Common;
using System.IO;

namespace Aliyeye.Car.Net.UI.AccountsUsersModel
{
    public partial class UserInfo : Aliyeye.Car.Net.UI.BaseForm
    {
        AccountsUsersSvc.SoapHeaderClass sopaHeaderUsers = new AccountsUsersSvc.SoapHeaderClass();
        Accounts_RolesSvc.SoapHeaderClass sopaHeaderRoles = new Aliyeye.Car.Net.UI.Accounts_RolesSvc.SoapHeaderClass();
        Base_DepartmentSvc.SoapHeaderClass sopaHeaderDepart = new Aliyeye.Car.Net.UI.Base_DepartmentSvc.SoapHeaderClass();

        private bool isDriver = false;
        private int userID = 0;
        DataSet dtDepart = null;
        DataSet dtRole = null;

        public UserInfo()
        {
            InitializeComponent();
        }

        public UserInfo(bool isDri, int id)
        {
            InitializeComponent();
            isDriver = isDri;
            userID = id;
            if (isDri)
            {
                this.Text = "驾驶员信息";
                groupBox1.Text = "驾驶员信息";
            }
            else
            {
                this.Text = "账号信息";
                groupBox1.Text = "账号信息";
                dtpDriveTime.Visible = false;
                label17.Visible = false;
                lblCarType.Visible = false;
                label32.Visible = false;
                label13.Visible = false;
            }
        }

        private void BindInfo()
        {
            //BLL.Accounts_Users bll = new BLL.Accounts_Users();
            //Model.Accounts_Users model = bll.GetModel(userID);
            //if (model != null)
            //{
            //    txtAddr.Text = model.Addr;
            //    txtEmail.Text = model.Email;
            //    txtHomeTel.Text = model.HomeTel;
            //    //txtPassword.Text = model.Password.ToString();
            //    txtQQ.Text = model.QQ;
            //    txtSchool.Text = model.School;
            //    txtTel.Text = model.Tel;
            //    txtTrueName.Text = model.TrueName;
            //    txtUserName.Text = model.UserName;
            //    cbbDepart.Text = GetDepartmentName(model.DepID);
            //    cbbRole.Text = GetRoleName(model.RoleId);
            //    cbbSex.Text = model.Sex;
            //    dtpBirth.Text = model.Birth;
            //    dtpDriveTime.Text = model.DriveTime.ToString("yyyy-MM-dd");
            //    dtpInputTime.Text = model.InputTime.ToString("yyyy-MM-dd");
            //}

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            AccountsUsersSvc.Accounts_Users model = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeaderUsers, userID),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (AccountsUsersSvc.Accounts_Users)result.Result;
                        txtAddr.Text = model.Addr;
                        txtEmail.Text = model.Email;
                        txtHomeTel.Text = model.HomeTel;
                        //txtPassword.Text = model.Password.ToString();
                        txtQQ.Text = model.QQ;
                        txtSchool.Text = model.School;
                        txtTel.Text = model.Tel;
                        txtTrueName.Text = model.TrueName;
                        txtUserName.Text = model.UserName;
                        cbbDepart.Text = GetDepartmentName(model.DepID);
                        cbbRole.Text = GetRoleName(model.RoleId);
                        cbbSex.Text = model.Sex;
                        dtpBirth.Text = model.Birth;
                        dtpDriveTime.Text = model.DriveTime.ToString("yyyy-MM-dd");
                        dtpInputTime.Text = model.InputTime.ToString("yyyy-MM-dd");

                        lblCarType.Text = model.CarType;
                        if (model.UserImage != null)
                            pictureBoxUser.Image = byteToBitmap(model.UserImage);
                        if (model.DriverImage != null)
                            pictureBoxDriver.Image = byteToBitmap(model.DriverImage);

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            if (!isDriver)
            {
                pictureBoxDriver.Visible = false;
                label17.Visible = false;
            }
        }

        public static Bitmap byteToBitmap(byte[] imageBytes)
        {
            if (imageBytes.Length == 0 || imageBytes.Length < 0)
            {
                return null;
            }

            string imageType = Encoding.ASCII.GetString(imageBytes, 20, 12);
            MemoryStream stream;
            if (imageType == "Bitmap Image")
            {
                //如果数据库中保存的是BMP图形则需要去掉OLE头
                stream = new MemoryStream(imageBytes, 78, imageBytes.Length - 78);
            }
            else
            {
                stream = new MemoryStream(imageBytes, 0, imageBytes.Length);
            }

            Bitmap bmp = new Bitmap(stream);

            return bmp;
        }

        private void AddUsers_Load(object sender, EventArgs e)
        {
            BindComboBoxInfo();
            if (userID != 0)
                BindInfo();
        }

        private void BindComboBoxInfo()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<Accounts_RolesSvc.Accounts_RolesServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeaderRoles, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            dtRole = (DataSet)result.Result;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message);
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );

            ServiceManipulator.SafeAsyncExecution<Base_DepartmentSvc.Base_DepartmentServiceSoapClient>(
                   proxy => proxy.GetList(sopaHeaderDepart, ""),
                   result =>
                   {
                       if (result.Error == null)
                       {
                           dtDepart = (DataSet)result.Result;
                           overlay.FadeOut();Cursor.Current = Cursors.Default;
                       }
                       else
                       {
                           PageCommon.Error(result.Error.Message);
                           overlay.FadeOut();Cursor.Current = Cursors.Default;
                       }
                   },
                   this
               );                 

            //BLL.Accounts_Roles bllRole = new BLL.Accounts_Roles();
            //string strWhere = string.Empty;

            //dtRole = bllRole.GetList(strWhere);

            //BLL.Base_Department bllDepart = new BLL.Base_Department();
            //dtDepart = bllDepart.GetList("");
        }

        //关闭
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string GetDepartmentName(int departID)
        {
            if (dtDepart != null)
            {
                DataRow[] drs = dtDepart.Tables[0].Select(" ClassID ='" + departID + "'");
                return drs[0]["ClassName"].ToString();
            }
            else
            {
                return "";
            }
        }

        private string GetRoleName(int roleID)
        {
            if (dtRole != null)
            {
                DataRow[] drs = dtRole.Tables[0].Select(" RoleID ='" + roleID + "'");
                return drs[0]["Description"].ToString();
            }
            else
            {
                return "";
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            AddUsers userForm = new AddUsers(isDriver, userID);
            userForm.ShowDialog();
            this.Close();
        }


    }
}
